/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.service;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.jiantong.user.enums.DepartmentType;
import com.jiantong.user.impl.dao.DeclareStationDao;
import com.jiantong.user.impl.dao.DepartmentDao;
import com.jiantong.user.model.DeclareStation;
import com.jiantong.user.model.Department;
import com.jiantong.user.service.DepartmentReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author : panxin
 */
@Slf4j
@Service
@RpcProvider
public class DepartmentReadServiceImpl implements DepartmentReadService {

    private final DepartmentDao departmentDao;
    private final DeclareStationDao declareStationDao;

    @Autowired
    public DepartmentReadServiceImpl(DepartmentDao departmentDao, DeclareStationDao declareStationDao) {
        this.departmentDao = departmentDao;
        this.declareStationDao = declareStationDao;
    }

    @Override
    public Response<Department> findDepartmentById(Long id) {
        try {
            Department dept = departmentDao.findById(id);
            if (dept == null) {
                log.error("department is not exists");
                return Response.fail("department.not.exists");
            }
            return Response.ok(dept);
        } catch (Exception e) {
            log.error("failed to find department by id = {}, cause : {}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.failed");
        }
    }

    @Override
    public Response<Optional<Department>> findOptionalDepartmentById(Long id) {
        try {
            return Response.ok(Optional.fromNullable(departmentDao.findById(id)));
        } catch (Exception e) {
            log.error("failed to find department by id = {}, cause : {}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.failed");
        }
    }

    @Override
    public Response<DeclareStation> findDeclareStationById(Long id) {
        try {
            DeclareStation station = declareStationDao.findById(id);
            if (station == null) {
                log.error("declare station is not exists");
                return Response.fail("declare.station.not.exists");
            }
            return Response.ok(station);
        } catch (Exception e) {
            log.error("failed to find declare station by id = {}, cause : {}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("find.declare.station.failed");
        }
    }

    @Override
    public Response<Optional<DeclareStation>> findOptionalDeclareStationById(Long id) {
        try {
            return Response.ok(Optional.fromNullable(declareStationDao.findById(id)));
        } catch (Exception e) {
            log.error("failed to find declare station by id = {}, cause : {}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("find.declare.station.failed");
        }
    }

    @Override
    public Response<List<Department>> findDepartmentsByGovernmentIdAndType(Long governmentId, DepartmentType type) {
        try {
            return Response.ok(departmentDao.findByGovernmentIdAndType(governmentId, type));
        } catch (Exception e) {
            log.error("failed to find department by governmentId = {}, type = {}, cause : {}",
                    governmentId, type, Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.failed");
        }
    }

    @Override
    public Response<List<Department>> findList(Map criteria) {
        try {
            return Response.ok(departmentDao.findList(criteria));
        } catch (Exception e) {
            log.error("failed to find department by governmentId = {}, type = {}, cause : {}", Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.failed");
        }
    }

    @Override
    public Response<List<String>> selectByOrgan(Map criteria) {
        try {
            return Response.ok(departmentDao.selectByOrgan(criteria));
        } catch (Exception e) {
            log.error("failed to find department by governmentId = {}, type = {}, cause : {}", Throwables.getStackTraceAsString(e));
            return Response.fail("find.department.failed");
        }
    }


}
